package com.xln.furn.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xln.furn.been.Furn;
import com.xln.furn.service.FurnService;
import com.xln.furn.service.UserService;
import com.xln.furn.userVO.User;
import com.xln.furn.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RestController
@Slf4j
public class FurnController {

    @Resource
    private FurnService furnService;
    @Resource
    private UserService userService;

    // @GetMapping("/queryById")
    // public Result queryById(Integer id){
    //     Furn furn = furnService.getById(id);
    //     System.out.println(furn);
    //     Result result = Result.success();
    //     result.setData(furn);
    //
    //     return result;
    // }

    @PostMapping("/save")
    public Result saveFurn(@Validated @RequestBody Furn furn, Errors errors){

        Map<String, Object> map = new HashMap<>();
        List<FieldError> fieldErrors = errors.getFieldErrors();
        for (FieldError fieldError : fieldErrors) {
            map.put(fieldError.getField(),fieldError.getDefaultMessage());
        }

        if (map.isEmpty()) {
            furnService.save(furn);
            Result<Object> success = Result.success();
            return success;
        }else {
            return Result.fail(400,"后端校验失败！",map);
        }
    }

    @GetMapping("/queryAll")
    public Result queryAll(){

        List<Furn> furns = furnService.list();
        return Result.success(furns);
    }

    @PostMapping("/queryById")
    public Result queryById(@RequestBody Furn furn){

        Furn furn1 = furnService.getById(furn.getId());
        return Result.success(furn1);
    }

    @PostMapping("/updateFurn")
    public Result updateFurn(@RequestBody Furn furn){
        furnService.updateById(furn);
        return Result.success();
    }

    @PostMapping("/deleFurn")
    public Result deleFurn(@RequestBody Furn furn){
        furnService.removeById(furn.getId());
        return Result.success();
    }
//前后端未分离
//    @RequestMapping("/pageFurn")
//    public Result pagFurn(@RequestParam(defaultValue = "1",required = false) Integer pageNum,
//                          @RequestParam(defaultValue = "5",required = false) Integer pageSize,HttpSession session){
////        User user =(User) session.getAttribute("user");
////        if (user == null){
////            return Result.fail(402,"未登入");
////        }
//        Page<Furn> furnPage = furnService.page(new Page<>(pageNum, pageSize));
//        return Result.success(furnPage);
//    }
    @RequestMapping("/pageFurn2")
    public Result pagFurn(
                            @RequestParam(defaultValue = "1", required = false) Integer pageNum,
                            @RequestParam(defaultValue = "5", required = false) Integer pageSize, HttpSession session) {
//        User user = (User) session.getAttribute("user");
        User user = LoginController.threadLocal.get();
        log.info("用户：{}", user);
        Page<Furn> furnPage = furnService.page(new Page<>(pageNum, pageSize));
        log.info("家具：{}", furnPage);
        return Result.success(furnPage);
    }


    @GetMapping("/queryByName2")
    public Result queryByName2(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String seach){

        QueryWrapper<Furn> queryWrapper = Wrappers.query();
        if (StringUtils.hasText(seach)){
            queryWrapper.like("name",seach);
        }

        Page<Furn> furnPage = furnService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(furnPage);
    }

    @GetMapping("/queryByName")
    public Result queryByName(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String seach){

        // QueryWrapper<Furn> queryWrapper = Wrappers.query();
        LambdaQueryWrapper<Furn> lambdaQueryWrapper = Wrappers.<Furn>lambdaQuery();

        if (StringUtils.hasText(seach)){
            lambdaQueryWrapper.like(Furn::getName,seach);
        }

        Page<Furn> furnPage = furnService.page(new Page<>(pageNum, pageSize), lambdaQueryWrapper);
        return Result.success(furnPage);
    }

    @PostMapping("/login")
    public Result login(@Validated @RequestBody User user, HttpSession session){
        String name = user.getName();
        String password = user.getPassword();
        log.info("用户名：{} 密码：{}", name,password);
        System.out.printf("用户名：%s 密码：%s", name,password);

        User dbuser = userService.getOne(Wrappers.<User>lambdaQuery().eq(User::getName, name));
        if (dbuser == null){
            return Result.fail(600,"用户不存在");
        }
        if (!password.equals(dbuser.getPassword())){
            return Result.fail(500,"密码不正确");

        }
        session.setAttribute("user",user);
        return Result.success();

    }

}
